import tkinter as tk
import tkinter.ttk as ttk
from pages.page_factory import PageFactory, PageType

#import ttkbootstrap


class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        #self.style = ttkbootstrap.Style(theme="pulse")
        self.title("pyTools v1.0.0")
        self.serial_port_page = None
        self.tcp_client_page = None
        self.tcp_server_page = None
        self.udp_client_page = None
        self.udp_server_page = None
        self.web_socket_client_page = None
        self.web_socket_server_page = None
        self.menu_bar = tk.Menu(self)
        self.init_menu_bar()
        self.config(menu=self.menu_bar)
        # Prevent window from resizing based on content
        self.pack_propagate(False)
        self.note_book = ttk.Notebook(self)
        self.init_pages()
        self.move_to_center()
        self.protocol("WM_DELETE_WINDOW", self.on_closing())

    def move_to_center(self):
        self.update_idletasks()  # Ensure window size is updated
        width = self.winfo_width()
        height = self.winfo_height()
        right = int(self.winfo_screenwidth() / 2 - width / 2)
        bottom = int(self.winfo_screenheight() / 2 - height / 2)
        self.geometry("+{}+{}".format(right, bottom))

    def init_menu_bar(self):
        self.init_file_menu()
        self.init_options_menu()
        self.init_help_menu()

    def init_file_menu(self):
        file_menu = tk.Menu(self.menu_bar, tearoff=0)
        self.menu_bar.add_cascade(label="File", menu=file_menu)
        file_menu.add_command(label="New Window", command=self.new_command)
        file_menu.add_command(label="Exit Application",
                              command=self.quit_command)

    def new_command(self):
        pass

    def quit_command(self):
        print("exiting application")
        self.quit()

    def init_options_menu(self):
        options_menu = tk.Menu(self.menu_bar, tearoff=0)
        self.menu_bar.add_cascade(label="Options", menu=options_menu)

        theme_menu = tk.Menu(options_menu, tearoff=0)
        options_menu.add_cascade(label="Application Theme", menu=theme_menu)
        theme_menu.add_command(
            label="Light Mode", command=self.light_mode_command)
        theme_menu.add_command(
            label="Dark Mode", command=self.dark_mode_command)

        language_menu = tk.Menu(options_menu, tearoff=0)
        options_menu.add_cascade(label="Language", menu=language_menu)
        language_menu.add_command(
            label="English", command=self.english_command)
        language_menu.add_command(label="简体中文", command=self.chinese_command)

    def light_mode_command(self):
        pass

    def dark_mode_command(self):
        pass

    def english_command(self):
        pass

    def chinese_command(self):
        pass

    def init_help_menu(self):
        help_menu = tk.Menu(self.menu_bar, tearoff=0)
        self.menu_bar.add_cascade(label="Help", menu=help_menu)
        help_menu.add_command(label="About", command=self.about_command)
        help_menu.add_command(label="User Manual",
                              command=self.user_manual_command)
        help_menu.add_command(
            label="Developer Manual", command=self.developer_manual_command
        )
        help_menu.add_separator()
        help_menu.add_command(
            label="Get Source from GitHub", command=self.get_source_from_github_command
        )
        help_menu.add_command(
            label="Get Source from Gitee", command=self.get_source_from_gitee_command
        )

    def about_command(self):
        pass

    def user_manual_command(self):
        pass

    def developer_manual_command(self):
        pass

    def get_source_from_github_command(self):
        pass

    def get_source_from_gitee_command(self):
        pass

    def init_pages(self):
        factory = PageFactory()
        # fmt: off
        self.serial_port_page = factory.create_page(PageType.SERIAL_PORT, self.note_book)
        self.tcp_client_page = factory.create_page(PageType.TCP_CLIENT, self.note_book)
        self.tcp_server_page = factory.create_page(PageType.TCP_SERVER, self.note_book)
        self.udp_client_page = factory.create_page(PageType.UDP_CLIENT, self.note_book)
        self.udp_server_page = factory.create_page(PageType.UDP_SERVER, self.note_book)
        self.web_socket_client_page = factory.create_page(PageType.WEB_SOCKET_CLIENT, self.note_book)
        self.web_socket_server_page = factory.create_page(PageType.WEB_SOCKET_SERVER, self.note_book)
        # fmt: on

        if self.serial_port_page is not None:
            self.note_book.add(self.serial_port_page, text="Serial Port")
        if self.tcp_client_page is not None:
            self.note_book.add(self.tcp_client_page, text="TCP Client")
        if self.tcp_server_page is not None:
            self.note_book.add(self.tcp_server_page, text="TCP Server")
        if self.udp_client_page is not None:
            self.note_book.add(self.udp_client_page, text="UDP Client")
        if self.udp_server_page is not None:
            self.note_book.add(self.udp_server_page, text="UDP Server")
        if self.web_socket_client_page is not None:
            self.note_book.add(self.web_socket_client_page, text="WebSocket Client")
        if self.web_socket_server_page is not None:
            self.note_book.add(self.web_socket_server_page, text="WebSocket Server")

        self.note_book.pack(expand=True, fill="both")

    @staticmethod
    def on_closing():
        print("closing")
